package DCSource.PluginsV2;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

import DCSource.ConfigManager.Item;
import DCSource.ConfigManager.MGm;
import DCSource.ConfigManager.Manager;
import DCSource.InfoTool;
import DCSource.dcsrc;

public class PLv2Manager {
	public PLv2Manager(){}
	private ArrayList<PluginV2> list = new ArrayList<PluginV2>();
	private MGm source = new MGm();
	public void loadFolder(String folder,boolean older){
		list.clear();
		source.clear();
		try{
			File[] jarFiles = new File(folder).listFiles(new FileFilter() {
		         public boolean accept(File f) {
		            return f.getName().toLowerCase().endsWith(".jar");
		         }
		      });
		 
		      URL[] urls = new URL[jarFiles.length];
		      for(int i=0; i<jarFiles.length; i++) {
		         urls[i] = jarFiles[i].toURI().toURL();
		      }
		 
		      ClassLoader loader = new URLClassLoader(urls);
		 
		      for(File jarFile : jarFiles) {            
		         JarInputStream jaris = new JarInputStream(new FileInputStream(jarFile));
		         JarEntry entry;            
		         while((entry = jaris.getNextJarEntry()) != null) {                
		            if(entry.getName().toLowerCase().endsWith(".class")) {
		               String className = entry.getName().substring(0, entry.getName().length() - 6).replace('/', '.');
		               Class<?> cls = loader.loadClass(className);
		               for(Class<?> i : cls.getInterfaces()) {
		            		  if(i.equals(PluginV2.class)) {
		            			  	try{
		            			  		PluginV2 getPlugin = (PluginV2)cls.newInstance();
		            			  		Manager newConfig = new Manager(getPlugin.getTitle());
		            			  		newConfig.addClass("start", true);
		            			  		newConfig.addItem("start", new Item("Package", entry.getName()),true);
		            			  		newConfig.addItem("start", new Item("AbsoluteFromfile", jarFile.getAbsolutePath()),true);
		            			  		newConfig.addItem("start", new Item("Fromfile", jarFile.getPath()),true);
		            			  		newConfig.addItem("start", new Item("File", jarFile.getName()),true);
		            			  		newConfig.addItem("start", new Item("Path", jarFile.getParent()),true);
		            			  		source.add(newConfig,true);
		            			  		list.add(getPlugin);
		            			   	}catch(Exception err){}
		            		   		break;
		            		  }
		               }                    
		            }
		         }
		      }
		      
		      for(int I=0;I<list.size();I++) {
		    	  if (list.get(I).getVersion()!=null){
			    	  if (dcsrc.isOlder(list.get(I).getVersion(),InfoTool.getVersion()) & older==false){
			    		  list.remove(I);
			    	  }
		    	  }
		      }
		}catch(Exception err){
		}
	}
	public ArrayList<PluginV2> getFromFile(String file,boolean older){
		ArrayList<PluginV2> returning =  new ArrayList<PluginV2>();
		try{
		File uri = new File(file);
		URL url = uri.toURI().toURL();
		ClassLoader loader = new URLClassLoader(new URL[]{url});
		JarInputStream jaris = new JarInputStream(new FileInputStream(uri));
        JarEntry entry;            
        while((entry = jaris.getNextJarEntry()) != null) {                
           if(entry.getName().toLowerCase().endsWith(".class")) {
              String className = entry.getName().substring(0, entry.getName().length() - 6).replace('/', '.');
              Class<?> cls = loader.loadClass(className);
              for(Class<?> i : cls.getInterfaces()) {
            	  if(i.equals(PluginV2.class)) {
            			try{
            				returning.add((PluginV2)cls.newInstance());}catch(Exception err){}
            			break;
            	  }
              }                    
           }
        }
        for (int I=0;I<returning.size();I++){
        	if (list.get(I).getVersion()!=null){
	        	if (dcsrc.isOlder(list.get(I).getVersion(),InfoTool.getVersion()) & older==false){
		    		  list.remove(I);
		    	}
        	}
        }
		}catch(Exception err){}
		return returning;
	}
	public ArrayList<PluginV2> toArrayList(){
		return list;
	}
	public MGm getInfo(){
		return source;
	}
}
